﻿using System;
using AccessControl.Contracts.Models;
using AccessControl.Contracts.Settings;
using System.Collections.Generic;
using AccessControl.Data.Models;

namespace AccessControl.Wpf.Settings.ViewModels
{
	class SensorsSettingsViewModel : SettingsBaseViewModel<ISensor>
	{
		#region Fields

		ISettingsService _settingsService;
		private static readonly SensorType[] _SensorTypes = new SensorType[] { SensorType.Biometric, SensorType.Buzzer, SensorType.CardsReader, SensorType.Lpr };

		#endregion

		#region Public Constructor

		public SensorsSettingsViewModel(ISettingsService settingsService) 
			: base(settingsService.Sensors)
		{
			Name = "Sensors";
			_settingsService = settingsService;
		}

		#endregion

		#region Public Properties

		public IList<SensorType> SensorTypes
		{
			get { return _SensorTypes; }
		}

		#endregion

		#region SettingsBaseViewModel Overrides

		public override void AddItem(ISensor item)
		{
			SettingsService.Add(item);
		}

		public override void EditItem(ISensor item)
		{
			SettingsService.Delete(item);
		}

		public override void DeleteItem(ISensor item)
		{
			SettingsService.Delete(item);
		}

		public override bool IsValidItem(ISensor item)
		{
			if (item.Ip == null)
			{
				MessagingService.Show("IP is not valid", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
				return false;
			}

			if (item.PortNumber <= 0)
			{
				MessagingService.Show("Port number is not valid", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
				return false;
			}

			if (item.RelatedEntryId == 0)
			{
				MessagingService.Show("No related entry is selected", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
				return false;
			}

			return true;
		}

		public override ISensor CreateNewItem()
		{
			return new Sensor()
			{
				Type = SensorType.Biometric
			};
		}

		#endregion
	}
}
